write_keyboard() {
    #[ -z "$keyboard_hacks" ] && return
    echo "Section \"ServerLayout\""
    echo "    Identifier \"keyboard\""
    echo "    InputDevice \"Keyboard0\" \"CoreKeyboard\""
    echo "    InputDevice \"Mouse0\" \"AlwaysCore\""
    echo "EndSection"
    
    echo "Section \"ServerFlags\""
    echo "    Option \"AutoAddDevices\" \"false\""
    echo "    Option \"AllowMouseOpenFail\" \"true\""
    echo "EndSection"

    echo "Section \"InputDevice\""
    echo "    Identifier \"Keyboard0\""
    echo "    Driver \"kbd\""
    $keyboard_hacks || true
    echo "EndSection"

    echo "Section \"InputDevice\""
    echo "    Identifier \"Mouse0\""
    echo "    Driver \"mouse\""
    echo "    Option \"Resolution\" \"${X_MOUSE_RESOLUTION:-"800"}\""
    echo "    Option \"Buttons\" \"${X_MOUSE_BUTTONS:-"3"}\""
    echo "EndSection"
}

write_screen() {
    num=${1:-0}
    #eval screen_hacks=\$screen_hacks_$num
    [ -z "$screen_hacks" ] && [ -z "$monitor_hacks" ] && [ -z "$display_hacks" ] && return
    echo "Section \"Screen\""
    echo "    Identifier \"Screen${num}\""
    if [ -n "$device_hacks" ]; then
	echo "    Device \"Device${num}\""
    fi
    if [ -n "$monitor_hacks" ]; then
        # force the monitor definition if revelent monitor settings are
        # desired, otherwise X.org may just use built-in defaults for monitor
        # settings
        echo "    Monitor \"Monitor${num}\""
    fi

    write_display || true

    for i in ${screen_hacks}; do
        ${i} || true
    done

    if [ "$VNC_MODULE_LOADED" ]; then
	echo "    Option \"httpdir\" \"/usr/share/x11vnc/classes\""
	echo "    Option \"PasswordFile\" \"/root/.vnc/passwd\""
	echo "    Option \"rfbport\" \"5900\""
	echo "    Option \"usevnc\""
    fi
    echo "EndSection"
}

write_monitor() {
    num=${1:-0}
    #eval monitor_hacks=\$monitor_hacks_$num
    #[ -z "$monitor_hacks" ] && return
    echo "Section \"Monitor\""
    echo "    Identifier \"Monitor${num}\""

    for i in ${monitor_hacks}; do
        ${i} || true
    done

    echo "EndSection"
}

write_device() {
    num=${1:-0}
    #eval device_hacks=\$device_hacks_$num
    [ -z "$device_hacks" ] && return
    echo "Section \"Device\""
    echo "    Identifier \"Device${num}\""

    for i in ${device_hacks}; do
        ${i} || true
    done

    echo "EndSection"
}

write_module() {
    [ -z "$module_hacks" ] && return
    echo "Section \"Module\""

    for i in ${module_hacks}; do
        ${i} || true
    done

    echo "EndSection"
}

write_display() {
    [ -z "$display_hacks" ] && return
    echo '    SubSection "Display"'
    for x in $display_hacks ; do
        ${x} || true
    done
    echo '    EndSubSection'
}

write_xorg_conf(){
    # For now, we will only do one screen, monitor, and device
    # under the assumption that multiple-head issues should
    # be handled exclusively by xrandr

    write_keyboard
    write_module
    write_screen 0
    write_monitor 0
    write_device 0
}
